import React, { Component } from 'react';
import { connect } from 'react-redux';
import { checkAuth } from "../actions/index";
import { LOGIN_LINK } from "../actions/types";
// import currentUserQuery from '../queries/current_user';

export default WrappedComponent => {
  class RequireAuth extends Component {
    componentWillMount(){
      this.props.checkAuth();
      // console.log(this.props)
    }
    componentWillUpdate(nextProps) {
      if (!nextProps.loading && !nextProps.user) {
        // console.log('push success');
        // this.props.history.replace('/login');
        this.props.history.replace(`${LOGIN_LINK}`);
      }
    }

    render() {
      return <WrappedComponent {...this.props} />;
    }
  }

  function mapStateToProps(state) {
    const { user, error, loading } = state.auth;
    return { user, error, loading };
  }

// export default connect(mapStateToProps, { loginUser })(Login);
  return connect(mapStateToProps, { checkAuth })(RequireAuth);
};
